<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<script>
  /*郭晓静*/
  let arr = [1, 3, 6, 2, 8, 9, 5];

  function mergeSort(arr) {
    if (arr.length <= 1) return arr;
    let midIndex = Math.floor(arr.length / 2);
    let leftArr = arr.slice(0, midIndex);
    let rightArr = arr.slice(midIndex);
    return merge(mergeSort(leftArr), mergeSort(rightArr))
  }

  function merge(left, right) {
    let result = [];
    while (left.length !== 0 && right.length !== 0) {
      if (left[0] > right[0]) {
        result.push(right.shift())
      }
      if (left[0] < right[0]) {
        result.push(left.shift())
      }
    }
    while (left.length !== 0) {
      result.push(left.shift())
    }
    while (right.length !== 0) {
      result.push(right.shift())
    }
    return result
  }

  console.log(mergeSort(arr))
</script>
</body>
</html>
